/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package anton.menu;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;


/**
 *
 * @author johan
 */

@ManagedBean(name = "listDinnerNames")
@SessionScoped

public class ListDinnerNames implements Serializable {

    @Resource(name = "jdbc/antonsDatasource")
    
    private DataSource dataSource;
    
     @ManagedProperty(value="#{destroy}")
     
     private Destroy destroy;
     
    private static final Logger LOG = Logger.getLogger(ListDinnerNames.class.getName());
   
    private Integer finemenuId;

    public Destroy getDestroy() {
        return destroy;
    }

    public void setDestroy(Destroy destroy) {
        this.destroy = destroy;
    }
       
    
    
    
    
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Integer getFinemenuId() {
        return finemenuId;
    }

    public void setFinemenuId(Integer finemenuId) {
        this.finemenuId = finemenuId;
    }
    
  
    private String dinnerName;

    private List<String> dinnerNameList= new ArrayList<>();
    
    private List<String> selectedValues;
    

    /**
     *
     * @return
     * @throws SQLException
     */
    
    public List<String> getSelectedValues() throws SQLException 
    {
        
       String sql="select dinner_name from FineDinnerMenu where finemenu_ID="+finemenuId;
        
        selectedValues = new ArrayList<>();
        
       if(finemenuId!=null){

        int i;

        if (dataSource == null) {
            throw new SQLException("Can't get data source");// throws exception i database not available
        }
        try (Connection con = dataSource.getConnection()) {
            if (con == null) {
                throw new SQLException("Can't get database connection");
            }
            try (PreparedStatement ps = con.prepareStatement(
                    sql)) {

                ResultSet result = ps.executeQuery();
                String dinnername;
                
                while (result.next()) {

                    dinnername = result.getString("dinner_name");

                    //adds dinnernames to list
                    this.selectedValues.add(dinnername);

                }

            }
        }
        }
        return selectedValues;
    }

    
    
    public void setSelectedValues(List<String> selectedValues) {
        this.selectedValues = selectedValues;
    }

    /**
     *
     * @param sql
     * @return
     * @throws SQLException
     */
    
    //Method gets dinners all dinners i database as an arraylist string
    public List<String> getDinnerNameList(String sql) throws SQLException { 

        if (dataSource == null) {
            throw new SQLException("Can't get data source");// throws exception i database not available
        }
        try (Connection con = dataSource.getConnection()) {
            if (con == null) {
                throw new SQLException("Can't get database connection");
            }
            PreparedStatement ps
                    = con.prepareStatement(sql); // query select from dinner specified attributes
            
            //get  item info from database
            ResultSet result = ps.executeQuery();
            
            dinnerNameList = new ArrayList<>();
            
            while (result.next()) {
                
                dinnerName = result.getString("dinner_name");
                
                //adds dinnernames to list
                dinnerNameList.add(dinnerName);
            }
        }
        return dinnerNameList;

    } 
    
    
    
     public List<String> getDinnerNameList() throws SQLException { 
         
         String sql="select dinner_name from Dinner";
        if (dataSource == null) {
            throw new SQLException("Can't get data source");// throws exception i database not available
        }
        try (Connection con = dataSource.getConnection()) {
            if (con == null) {
                throw new SQLException("Can't get database connection");
            }
            PreparedStatement ps
                    = con.prepareStatement(sql); // query select from dinner specified attributes
            
            //get  item info from database
            ResultSet result = ps.executeQuery();
            
            dinnerNameList = new ArrayList<>();
            
            while (result.next()) {
                
                dinnerName = result.getString("dinner_name");
                
                //adds dinnernames to list
                dinnerNameList.add(dinnerName);
            }
        }
        return dinnerNameList;

    }
    
    
    
    /**
     *
     * @param sql
     * @param fineMenuId
     * @return
     * @throws SQLException
     */
    
    
    public  String add() throws SQLException {
       
        //sql query insert values into database    
        
       String sql= "insert into FineDinnerMenu (dinner_name,finemenu_id) values (?,?);";
        
       String delete="delete from FineDinnerMenu where finemenu_ID="+finemenuId;
        
      
        destroy.destroy(delete);
       
       
       if(dataSource==null)
			throw new SQLException("Can't get data source");// throws exception i database not available
        try (Connection con = dataSource.getConnection()) {
            
            if(con==null)
                throw new SQLException("Can't get database connection");
            
            try (PreparedStatement ps = con.prepareStatement(sql)){
              
                for(String dinnerName : selectedValues) 
                {
                    
                ps.setString( 1, dinnerName );
                ps.setInt(2, finemenuId);
                
                // performs insertion
                 int i= ps.executeUpdate(); 
                
                }         
            }
            
            this.selectedValues=null;
            con.close();
                      
        }
         
        
         return "/admin/dinnermenu/createmenu?faces-redirect=true";  
    
    }
       
    
    

}
